﻿using System.Linq;
using Notoric.Commanding.CommandBase;

namespace Notoric.ViewModel
{
    /// <summary>
    /// An undoable command for deleting a section.
    /// </summary>
    public class DeleteSectionCommand : UndoableCommandBase
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DeleteSectionCommand"/> class.
        /// </summary>
        /// <param name="undoStack">The undo command stack</param>
        /// <param name="redoStack">The redo command stack</param>
        public DeleteSectionCommand(CommandStack undoStack, CommandStack redoStack)
            : base(undoStack, redoStack)
        {
            AddParameter<Elements.Section>("Section", ParameterDirection.In);

            Name = "Delete Section";
        }

        #endregion

        #region Command Logic

        /// <inheritdoc/>
        protected override void CommandOperation(object parameter)
        {
            if (Section.IsSelected)
                Section.FindOwner<Elements.Page>().ClearSelection();
            var modelSection = Section.AdaptedElement as Model.Elements.SectionElement;
            modelSection.FindOwner<Model.Elements.PageElement>().Sections.Remove(modelSection);

            Section.FindOwner<Elements.Project>().IsModified = true;
        }

        /// <inheritdoc/>
        protected override void RedoOperation()
        {
            if (Section.IsSelected)
                Section.FindOwner<Elements.Page>().ClearSelection();
            var modelSection = Section.AdaptedElement as Model.Elements.SectionElement;
            modelSection.FindOwner<Model.Elements.PageElement>().Sections.Remove(modelSection);

            Section.FindOwner<Elements.Project>().IsModified = true;
        }

        /// <inheritdoc/>
        protected override void UndoOperation()
        {
            var modelSection = Section.AdaptedElement as Model.Elements.SectionElement;
            modelSection.FindOwner<Model.Elements.PageElement>().Sections.Add(modelSection);
            var page = Section.FindOwner<Elements.Page>();
            var vmSection = page.Sections.First(s => s.AdaptedElement == modelSection);
            page.Sections[page.Sections.IndexOf(vmSection)] = Section;

            Section.FindOwner<Elements.Project>().IsModified = true;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the deleted section.
        /// </summary>
        /// <value>
        /// <para>
        /// Type: <see cref="Elements.Section"/>
        /// </para>
        /// <para>
        /// The section that will be deleted by the command.<br/>
        /// This property wraps the "Section" command parameter.
        /// </para>
        /// </value>
        public Elements.Section Section
        {
            get { return GetParameterValue<Elements.Section>("Section"); }
            set { SetParameterValue<Elements.Section>("Section", value); }
        }

        #endregion
    }
}
